﻿Imports System.Data.SqlClient

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindGV()
    End Sub
   

    'private void BindGV()
    '{
    '    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["newconn"].ConnectionString))
    '    {

    '        try
    '        {
    '            conn.Open();
    '            SqlCommand cmd = new SqlCommand("SELECT * FROM Products", conn);
    '            SqlDataReader reader;
    '            reader = cmd.ExecuteReader();
    '            gv.DataSource = reader;
    '            gv.DataBind();
    '        }
    '         catch(Exception ex)
    '        {

    '             Response.Write("Error:" + ex.Message.ToString());

    '        }
    '        finally 
    '        {
    '            conn.Close();
    '        }


    '    }


    '}

    Dim conn As SqlConnection
    Private Sub BindGV()
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("newconn").ConnectionString)
            Dim sql As String = "SELECT * FROM Products"
            conn.Open()
            Dim dataadapter As New SqlDataAdapter(sql, conn)
            Dim ds As New DataSet()

            dataadapter.Fill(ds)
            gv.DataSource = ds
            gv.DataBind()
        End Using

      
        

    End Sub

    Protected Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        Dim link As LinkButton
        Dim gvr As GridViewRow
        Dim rowIndex As Integer
        If e.CommandName = "EditRow" Then
            link = CType(e.CommandSource, LinkButton)
            gvr = CType(link.NamingContainer, GridViewRow)
            rowIndex = gvr.RowIndex
            gv.EditIndex = rowIndex
            BindGV()
        End If
    End Sub
End Class